<?php

class Zendvn_Cache
{
	var $cache_dir = '';
	var $compress = array();
	var $lang;
	
	public function __construct()
	{
		$this->cache_dir = CACHE_PATH;
		
		// compress cache type
		if ( function_exists( 'gzcompress' ) and function_exists( 'gzuncompress' ) )
		{
			$this->compress = array( 
				'gzcompress', 'gzuncompress' 
			);
		}
		elseif ( function_exists( 'gzdeflate' ) and function_exists( 'gzinflate' ) )
		{
			$this->compress = array( 
				'gzdeflate', 'gzinflate' 
			);
		}
	}
	 
	/**
	 * set_cache()
	 * 
	 * @param mixed $filename
	 * @param mixed $content
	 * @return
	 */
	public function set_cache( $filename, $content )
	{
		if ( empty( $filename ) or ! preg_match( "/(.*)\.cache/", $filename ) ) return false;
		$filename = basename( $filename );
	
		return $this->gz_put_contents( $this->cache_dir . "/" . $filename, $content );
	}
	
	/**
	 * get_cache()
	 * 
	 * @param mixed $filename
	 * @return
	 */
	public function get_cache( $filename )
	{
		if ( empty( $filename ) or ! preg_match( "/(.*)\.cache/", $filename ) ) return false;
		$filename = basename( $filename );
		if ( ! file_exists( $this->cache_dir . "/" . $filename ) ) return false;
		return $this->gz_get_contents( $this->cache_dir . "/" . $filename );
	}

	/**
	 * gz_get_contents()
	 * 
	 * @param mixed $filename
	 * @return
	 */
	private function gz_get_contents( $filename )
	{	
		$content = file_get_contents( $filename );
	
		if ( isset( $this->compress ) and ! empty( $this->compress ) )
		{
			$content = call_user_func( $this->compress[1], $content );
		}
	
		return $content;
	}

	/**
	 * gz_put_contents()
	 * 
	 * @param mixed $filename
	 * @param mixed $content
	 * @return
	 */
	private function gz_put_contents( $filename, $content )
	{
		if ( isset( $this->compress ) and ! empty( $this->compress ) )
		{
			$content = call_user_func( $this->compress[0], $content, 9 );
		}
	
		return file_put_contents( $filename, $content, LOCK_EX );
	}
	
	/**
	 * load()
	 * 
	 * @param mixed $result
	 * @return
	 */
	public function load($modename)
	{	
		$list = array();
		$cache_file = $modname . ".cache";
		if ($cache = $this->get_cache($cache_file) != false)
		{
			$list = unserialize( $cache );
			return $list;
		}	
		return false;
	}
	
	public function save($result, $modname = '')
	{
		if ( empty( $result ) ) return false;
		$cache_file = $modname . "_cache.cache";
		$cache = serialize($result);
		$this->set_cache( $cache_file, $cache );
		return true;
	}
	
	/**
	 * scanCacheDir()
	 * 
	 * @param mixed $directory
	 * @param mixed $pattern
	 * @param integer $sorting_order
	 * @return
	 */
	public function scanCacheDir( $directory, $pattern, $sorting_order = 0 )
	{
		$return = array();
		if ( is_dir( $directory ) )
		{
			$files = @scandir( $directory, $sorting_order );
			if ( ! empty( $files ) )
			{
				foreach ( $files as $file )
				{
					if ( $file != "." and $file != ".." and $file != ".htaccess" and $file != "index.html" )
					{
						if ( ! is_array( $pattern ) )
						{
							if ( preg_match( $pattern, $file ) ) $return[] = $file;
						}
						else
						{
							foreach ( $pattern as $p )
							{
								if ( preg_match( $p, $file ) )
								{
									$return[] = $file;
									break;
								}
							}
						}
					}
				}
			}
		}
	
		return $return;
	}

	/**
	 * deleteFile()
	 * 
	 * @param mixed $file
	 * @param bool $delsub
	 * @return
	 */
	private function deleteFile( $file )
	{
		if (file_exists($file)) {
			return @unlink($file);
		}
		return false;
	}

	/**
	 * delete()
	 * 
	 * @param mixed $pattern
	 * @return
	 */
	private function delete( $pattern )
	{
		$files = $this->scanCacheDir( $this->cache_dir, $pattern );
	
		if ( ! empty( $files ) )
		{
			foreach ( $files as $f )
			{
				$this->deleteFile( $this->cache_dir . "/" . $f );
			}
		}
	}
	
	/**
	 * nv_delete_all_cache()
	 * 
	 * @return
	 */
	public function removeall()
	{
		$pattern = "/(.*)\.cache/";
		$this->delete( $pattern );
	}
	
	/**
	 * delModuleCache()
	 * 
	 * @return void
	 */
	public function remove($module_name)
	{
		$pattern = "/^" . $module_name . "\_(.*)\.cache$/i";
		$this->delete( $pattern );
	}
}